﻿using EFCore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SService;

namespace WMSWeb.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventorytypeController : ControllerBase
    {
        private readonly WmssystemContext myDb;
        private readonly InventorytypeS inventorytypeS;

        public InventorytypeController(WmssystemContext myDb, SService.InventorytypeS  inventorytypeS)
        {
            this.myDb = myDb;
            this.inventorytypeS = inventorytypeS;
        }
        /// <summary>
        /// 查询出入库类型的全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<List<Sys_Inventorytype>> Iqu()
        {
            return inventorytypeS.Iqu();
        }
        /// <summary>
        /// 查询出库类型的全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<List<Sys_Inventorytype>> Iqu1()
        {
            return inventorytypeS.Iqu1();
        }
        /// <summary>
        /// 出入库类型分页模糊查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("FenYe")]
        public async Task<List<Sys_Inventorytype>> FenYe(int pageSize, int currentPage, string Key = null, bool? Status = null, bool? Direction = null, string Operator = null)
        {
            return await inventorytypeS.FenYe(pageSize, currentPage, Key, Status, Direction, Operator);
        }
        /// <summary>
        /// 出入库类型添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<int>> Add(Sys_Inventorytype typeAdd)
        {
            // 为实体的属性赋值
            typeAdd.I_TOperatorTime = DateTime.Now;
            typeAdd.I_TIsDelete = true;

            // 调用服务层的方法执行添加操作
            //return await Sys_InventorytypeS.Add(typeAdd);
            typeAdd.I_TTypeCode = GenerateUniquePositionCode();

            // 调用服务层的添加方法
            var result = await inventorytypeS.Add(typeAdd);

            return result;
        }
        /// <summary>
        /// 出入库类型修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<int>> Update(Sys_Inventorytype typeUpd)
        {
            return await inventorytypeS.Upd(typeUpd);
        }
        /// <summary>
        /// 出入库类型逻辑删除
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdIsDelete")]
        public async Task<ActionResult<int>> UpdIsDelete(int id, bool isDeleted)
        {
            return await inventorytypeS.UpdIsDelete(id, isDeleted);
        }
        //自动生成编号
        private string GenerateUniquePositionCode()
        {
            string prefix = "GWBH"; // 前缀
            string timestamp = DateTime.Now.ToString("yyyyMMdd"); // 时间戳
            string randomSuffix = GenerateRandomNumericSuffix(4); // 随机后缀

            return $"{prefix}{timestamp}{randomSuffix}";
        }
        private string GenerateRandomNumericSuffix(int length)
        {
            Random random = new Random();
            const string chars = "0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }
    }
}
